package com.wx.app.mylib.utils;


import org.ocpsoft.prettytime.PrettyTime;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**Created on 14-6-28.
 */
public class TimeHelper {

    public static PrettyTime prettyTime = new PrettyTime();

    public static String getCurrDate() {
        Date date = new Date(System.currentTimeMillis());
        return getDate(date);
    }
    public static String getCurrDateHm() {
        Date date = new Date(System.currentTimeMillis());
        return getHmDate(date);
    }
    public static String getDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        return format.format(date);
    }

    public static String getHmDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }

    public static String millisecs2DateString(long timestamp) {
        long gap = System.currentTimeMillis() - timestamp;
        if (gap < 1000 * 60 * 60 * 24) {
            String s = prettyTime.format(new Date(timestamp));
            // return s.replace(" ", "");
            return s;
        } else {
            return getDate(new Date(timestamp));
        }
    }

    public static boolean haveTimeGap(long lastTime, long time) {
        int gap = 1000 * 60 * 3;
        return time - lastTime > gap;
    }

    /**
     * 通过时间戳获取 “12:30” 格式时间
     *
     * @param timeMillis
     * @return
     */
    public static String getTimeHH(long timeMillis) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(timeMillis));
    }

    private SimpleDateFormat sdf = null;

    /*将字符串转为时间戳*/

    public static long getStringToDate(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        try {
            date = sdf.parse(time);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    /*时间戳转换成字符窜*/
    public static String getDateToString(long time) {
        Date d = new Date(time);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }
    /*
        时间戳转换成星期
     */
    public static String getWeek(long timeStamp) {
        int mydate = 0;
        String week = null;
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(timeStamp));
        mydate = cd.get(Calendar.DAY_OF_WEEK);
        // 获取指定日期转换成星期几
        if (mydate == 1) {
            week = "星期日";
        } else if (mydate == 2) {
            week = "星期一";
        } else if (mydate == 3) {
            week = "星期二";
        } else if (mydate == 4) {
            week = "星期三";
        } else if (mydate == 5) {
            week = "星期四";
        } else if (mydate == 6) {
            week = "星期五";
        } else if (mydate == 7) {
            week = "星期六";
        }
        return week;
    }

}
